package curtain.whynot.frame.basic.csv.metadata;

import com.baomidou.mybatisplus.toolkit.GlobalConfigUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: wuyichao
 * @Description:
 * @Date: Created in 10:59 2018/7/23.
 * @Modified:
 */
public class MetadataUtil {

    public static List<ColumnInfo> getTableColumnInfos(Class clzName ,String tableName) throws SQLException {
        ResultSet rs = null;
        ResultSet rs2 = null;
        Connection conn = null;
        DatabaseMetaData dmd = null;
        List<ColumnInfo> columnInfos = new ArrayList<ColumnInfo>();
        SqlSession sqlSession = null;
        try {

            SqlSessionFactory sqlSessionFactory = GlobalConfigUtils.currentSessionFactory(clzName);

             sqlSession = sqlSessionFactory.openSession();

//            sqlSession = SqlHelper.sqlSession(clzName);
            conn = sqlSession.getConnection();
            dmd = conn.getMetaData();
            rs = dmd.getColumns(null, "%", tableName, "%");
            rs2 = dmd.getPrimaryKeys(null, "%", tableName);
            final List<String> pkCols = getPkColumnInfos(rs2);
            while (rs.next()) {
                final String strFieldName = rs
                        .getString(ColumnInfo.COLUMN_NAME);
                final String strFieldType = rs.getString(ColumnInfo.TYPE_NAME);
                final int nullable = rs.getInt(ColumnInfo.NULLABLE);
                final String fieldLabel = rs.getString(ColumnInfo.REMARKS);
                final int precision = rs.getInt(ColumnInfo.DECIMAL_DIGITS);
                final int colSize = rs.getInt(ColumnInfo.COLUMN_SIZE);
                final int charLen = rs.getInt(ColumnInfo.CHAR_OCTET_LENGTH);
                ColumnInfo columnInfo = new ColumnInfo() {
                    @Override
                    public boolean isPk() {
                        return pkCols.contains(strFieldName);
                    }

                    @Override
                    public String getColumnLabel() {
                        return fieldLabel;
                    }

                    @Override
                    public int getColumnSize() {
                        return colSize;
                    }

                    @Override
                    public String getColumnName() {
                        return strFieldName;
                    }

                    @Override
                    public String getColTypeName() {
                        return strFieldType;
                    }

                    @Override
                    public int getColPrecision() {
                        return precision;
                    }

                    @Override
                    public int getColScale() {
                        return 0;
                    }

                    @Override
                    public int getLength() {
                        return charLen;
                    }

                    @Override
                    public int isNullable() {
                        return nullable;
                    }

                    @Override
                    public String toString() {
                        return  strFieldName
                                +">>>"+ this.isPk() + ">>>" + getColPrecision()
                                + ">>>" + getColumnSize() + ">>>" + ">>>"
                                + strFieldType + ">>>" + nullable + ">>>"
                                + getColPrecision()  + ">>>"+fieldLabel;
                    }
                };
                columnInfos.add(columnInfo);
            }
        } catch (Exception e){
            throw new RuntimeException(e.getMessage());
        } finally{

            if(null != conn){
                conn.close();
            }

            if(null != sqlSession){
                sqlSession.close();
            }

        }
        return columnInfos;
    }

    private static List<String> getPkColumnInfos(ResultSet rs) throws SQLException {
        List<String> pkCols = new ArrayList<String>();
        while (rs.next()) {
            String strFieldName = rs.getString(ColumnInfo.COLUMN_NAME);
            pkCols.add(strFieldName);
        }
        return pkCols;
    }
}
